package com.bweab.controll;

import com.bweab.pojo.en.UserDepositDormitoryEn;
import com.bweab.service.api.UserDepositDormitoryEnServiceInterface;
import com.bweab.service.api.UsersTbServiceInterface;
import com.bweab.service.impl.UserDepositDormitoryEnServiceInterfaceImpl;
import com.bweab.service.impl.UsersTbServiceInterfaceImpl;
import com.bweab.util.JSONStats;
import com.bweab.util.PageList;
import com.bweab.util.WebUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;
@WebServlet("/userQueryAll")
public class UserQuerryAll extends HttpServlet {

    //定义结果
    PageList userQueryResult = new PageList();
    //定义服务
    //三表查询

    //定义页数变量
    int totalPage;
    UserDepositDormitoryEnServiceInterface userDepositDormitoryEnService=new UserDepositDormitoryEnServiceInterfaceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数
        int userId = Integer.parseInt(req.getParameter("userId"));
        //获取当前页
        int page= Integer.parseInt(req.getParameter("page"));
        //获取取值个数
       int pageSize= Integer.parseInt(req.getParameter("pageSize"));

        System.out.println(userId);
        System.out.println(page);
        System.out.println(pageSize);


        //调用服务层
        //结果为所有对象的集合
        List<UserDepositDormitoryEn> userDepositDormitoryEnList =userDepositDormitoryEnService.getUserDepositDormitryAll(userId, page, pageSize);

        if (userDepositDormitoryEnList != null) {
            if(userDepositDormitoryEnList.size()>0){
                int totalCount= userDepositDormitoryEnList.get(0).getTotalCount();//获取总数
                //计算页数
                if(totalCount%pageSize==0){
                    //计算总页数
                    totalPage= totalCount/pageSize;

                }else {
                    totalPage=totalCount/pageSize+1;
                }
                //设置页数相关
                userQueryResult.setPage(page);
                userQueryResult.setPageTotal(totalPage);
                userQueryResult.setPageSize(pageSize);

                //设置响应数据
                userQueryResult.setStatusCode(JSONStats.QUERY_SUCCESS.getStatusCode());
                userQueryResult.setPageList(userDepositDormitoryEnList);
                userQueryResult.setMessage(JSONStats.QUERY_SUCCESS.getMessage());
            }else {
                //查询失败
                userQueryResult.setStatusCode(JSONStats.QUERY_ERROR.getStatusCode());
                userQueryResult.setMessage(JSONStats.QUERY_ERROR.getMessage());
            }

        }else{
            userQueryResult.setStatusCode(JSONStats.QUERY_ERROR.getStatusCode());
            userQueryResult.setMessage(JSONStats.QUERY_ERROR.getMessage());
        }
        //发送数据
        WebUtil.writeJson(resp,userQueryResult);

    }
}
